package tools;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

@ContextConfiguration(locations = { "classpath:applicationContext_test.xml" })
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class BaseServiceDatabaseTestSupport extends
		AbstractTransactionalJUnit4SpringContextTests {

	@Autowired
	protected SessionFactory sessionFactory;

	protected JdbcTemplate jdbcTemplate;

	@Override
	@Autowired
	public void setDataSource(DataSource dataSource) {
		super.setDataSource(dataSource);
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Before
	public void setUp() throws Exception {
		executeSqlScript("classpath:/schema/H2.schema.sql", false);
		// executeSqlScript("classpath:/schema/MySQL.schema.sql", true);
	}

	protected void clear() {
		sessionFactory.getCurrentSession().clear();
	}

	protected void flush() {
		sessionFactory.getCurrentSession().flush();
	}
}
